package fbp
{
	import mx.managers.ISystemManager;
	
	
	public class NetworkComponent implements INetworkComponent
	{
		public function NetworkComponent(system_manager:ISystemManager)
		{
			this._system_manager = system_manager;
		}
		
		var _data_items_to_process:Array = new Array();
		var _processed_data:Array = new Array();
		var _outputs:Array = new Array();
		var _system_manager:ISystemManager;

		public function output_to(component:INetworkComponent):void
		{
			if(component == null)
			{
				throw new Error("Invalid INetworkComponent provided.");
			}
			
			this._outputs.push(component);
		}
		
		public function add_data(data)
		{
			//This should be LIFO instead of FIFO
			//Change to queue
			this._data_items_to_process.push(data);
		}
		
		public function get_data()
		{
			if(this._processed_data.length > 0)
			{
				return this._processed_data.pop();
			}
		}
		
		public function start_processing():void
		{
			while(_data_items_to_process.length > 0)
			{
				var provided_data:* = _data_items_to_process.pop();
				new PseudoThread(this._system_manager, handle_completed_processing, provided_data);
			}
		}
		
		public function handle_completed_processing(data)
		{
			var returned_data = process(data);
			_processed_data.push(returned_data);
		}
		
		public function has_processed_data():Boolean
		{
			return this._processed_data.length > 0;
		}
		
		public virtual function process(data:*):*
		{
			return null;
		}
		
	}
}